package com.buding.fx.dao.impl;

import com.buding.fx.dao.MenuDao;
import com.buding.fx.dao.PropertyDao;
import com.buding.fx.entity.Menu;
import com.buding.fx.entity.Property;
import com.buding.fx.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mingdongwei-jwk
 * @date 2022/10/09 10:44
 * @class com.buding.fx.dao.impl
 * @Description
 */
public class PropertyDaoImpl implements PropertyDao {

    @Override
    public int insert(Property property) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            r = mapper.insert(property);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public Property selectByKey(String key) {
        Property property = null;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            property = mapper.selectByKey(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return property;
    }

    @Override
    public int update(Property property) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            r = mapper.update(property);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public int updateByKey(Property property) {
        int r = 0;
        SqlSession session = null;
        try {
             session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            r = mapper.updateByKey(property);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public int delete(String key) {
        int r = 0;
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            r = mapper.delete(key);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return r;
    }

    @Override
    public List<Property> selectAll() {
        List<Property> list = new ArrayList<>();
        SqlSession session = null;
        try {
            session = MyBatisUtil.getSqlSession();
            PropertyDao mapper = session.getMapper(PropertyDao.class);
            list = mapper.selectAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (session!=null){
                session.close();
            }
        }
        return list;
    }
}
